<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="avicit.platform6.commons.utils.ViewUtil"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="X-UA-Compatible" content="IE=8">
	<base href="<%=ViewUtil.getRequestPath(request)%>">
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<link href="static/js/platform/component/jQuery/jquery-easyui-1.3.5/themes/blue/easyui.css" type="text/css" rel="stylesheet">
	<link href="static/js/platform/component/jQuery/jquery-easyui-1.3.5/themes/blue/page.css" type="text/css"  rel="stylesheet">
	<link href="avicit/im/common/im_themes_extend/im_icon.css" type="text/css" rel="stylesheet">
	<link href="static/js/concui/concuiDataTable/concuiDataTable.css" type="text/css" rel="stylesheet">
	<title>组成项生成</title>
	<style type="text/css">
		html,body{height:100%;overflow:hidden;}
		.itemSelectTable{
			width:100%;
		}
		.itemSelectTable td{
			text-align:center;
			padding:4px 0px;
		}
		.plusSign{
			color:#FA0000;
			font-weight:bold;
			font-size:19px;
		}
		.itemSelectTable td input,.itemSelectTable td select{
			width:100%;
		}
		.itemButtonTable{
		}
		.itemButtonTable td{
			padding:5px 0px;
		}
		.itemButtonTable button{
			width:50px;
			border:solid 1px #3C7FB1;
			padding:2px;
		}
	</style>
</head>
<body>
	<div id="toolbar" class="datagrid">
		<div class="datagrid-toolbar">
			<a class="easyui-linkbutton l-btn l-btn-plain" plain="true" iconcls="icon-im-save" onclick="accept();" href="javascript:void(0);" id="btnAccept" group=""><span class="l-btn-left"><span class="icon-im-save"></span><span class="l-btn-text">确定</span></span></a>
			<a class="easyui-linkbutton l-btn l-btn-plain" plain="true" iconcls="icon-im-newhighset" onclick="highSet();" href="javascript:void(0);" id="btnHighSet" group=""><span class="l-btn-left"><span class="icon-im-newhighset"></span><span class="l-btn-text">高级</span></span></a>
			<a class="easyui-linkbutton l-btn l-btn-plain" plain="true" iconcls="icon-im-newshareset" onclick="reset();" href="javascript:void(0);" id="btnReset" style="display:none;"><span class="l-btn-left"><span class="icon-im-newshareset"></span><span class="l-btn-text">重置</span></span></a>
			<a class="easyui-linkbutton l-btn l-btn-plain" plain="true" iconcls="icon-im-newsubmitidentify" onclick="valid();" href="javascript:void(0);" id="btnValid" style="display:none;"><span class="l-btn-left"><span class="icon-im-newsubmitidentify"></span><span class="l-btn-text">验证</span></span></a>
			<a class="easyui-linkbutton l-btn l-btn-plain" plain="true" iconcls="icon-im-back" onclick="closeWindow();" href="javascript:void(0);" id="btnClose" group=""><span class="l-btn-left"><span class="icon-im-back"></span><span class="l-btn-text">返回</span></span></a>
		</div>
	</div>
	<div id="normalSet" style="position:relative;">
		<div class="panel-body" style="padding:4px;width:244px;overflow:hidden;float:left;">
			<div style="width:240px">
				<table class="itemSelectTable">
					<tr>
						<td>
							<select id="col1" onchange="colSelectChange(this);"></select>
						</td>
					</tr>
					<tr>
						<td>
							<span class="plusSign">+</span>
						</td>
					</tr>
					<tr>
						<td>
							<input id="staticStr1" class="easyui-validatebox" type="text" onchange="colSelectChange(this);"/>
						</td>
					</tr>
					<tr>
						<td>
							<span class="plusSign">+</span>
						</td>
					</tr>
					<tr>
						<td>
							<select id="col2" onchange="colSelectChange(this);"></select>
						</td>
					</tr>
					<tr>
						<td>
							<span class="plusSign">+</span>
						</td>
					</tr>
					<tr>
						<td>
							<input id="staticStr2" class="easyui-validatebox" type="text"  onchange="colSelectChange(this);"/>
						</td>
					</tr>
					<tr>
						<td>
							<span class="plusSign">+</span>
						</td>
					</tr>
					<tr>
						<td>
							<select id="col3" onchange="colSelectChange(this);"></select>
						</td>
					</tr>
				</table>
			</div>
		</div>
		<div style="margin-left:252px;">
			<div style="padding-left:10px;padding-right:4px;">
				<div style="padding-top:6px;">
					<span class="plusSign">=:</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:blue;font-weight:bold;;">${cbbTabcolumns.colLabel}</span>
				</div>
				<div style="overflow:hidden;height:6px;line-height:0px;font-size:0px;padding:0px;margin:0px;"></div>
				<div class="panel-header" style="background:#fff;">
					<div><span style="color:blue;">规则描述：</span></div>
					<div style="padding-top:5px;">
						<textarea id="showRule" style="width:100%;height:197px;" class="easyui-validatebox" readonly="readonly">${cbbTabcolumns.colLabel}:=${cbbTabcolumns.colGeneMethodRuleText}</textarea>
					</div>
				</div>
			</div>
		</div>
	</div>
	<div id="highSet" style="display:none;">
		<div id="highSetTop">
			<div style="padding:4px;width:104px;overflow:hidden;float:left;padding-bottom:2px;">
				<div style="width:240px">
					<div><span style="color:blue;">${cbbTabcolumns.colLabel}:=</span></div>
				</div>
				<div style="padding-top:3px;">
					<table class="itemButtonTable">
						<tr>
							<td><button id="btnAdd" onclick="setRuleHighText(' + ')">+</button></td>
							<td style="width:5px;"></td>
							<td><button onclick="setRuleHighText(' - ')">-</button></td>
						</tr>
						<tr>
							<td><button onclick="setRuleHighText(' * ')">*</button></td>
							<td style="width:5px;"></td>
							<td><button onclick="setRuleHighText(' / ')">/</button></td>
						</tr>
						<tr>
							<td><button onclick="setRuleHighText(' ( ')">(</button></td>
							<td style="width:5px;"></td>
							<td><button onclick="setRuleHighText(' ) ')">)</button></td>
						</tr>
						<tr>
							<td><button onclick="setRuleHighText(' && ')">并且</button></td>
							<td style="width:5px;"></td>
							<td><button onclick="setRuleHighText(' || ')">或者</button></td>
						</tr>
					</table>
				</div>
			</div>
			<div style="margin-left:110px;">
				<div style="padding-left:5px;padding-right:4px;">
					<div style="padding-top:3px;">
						<textarea id="showRuleHigh" style="width:100%;height:135px;" class="easyui-validatebox">${cbbTabcolumns.colGeneMethodRuleText}</textarea>
					</div>
				</div>
			</div>
			<div style="clear:both;height:0px;font-size:0px;line-height:0px;padding:0px;margin:0px;"></div>
		</div>
		<div>
			<div style="padding:4px;width:200px;overflow:hidden;float:left;padding-top:0px;">
				<div id="funcTableOuter" class="concui-datatable-outer">
					<table id="funcTable" class="concui-datatable">
						<thead>
							<th style="width:150px;text-align:center;">标准函数</th>
							<th style="width:150px;text-align:center;display:none;">函数名称</th>
						</thead>
					</table>
				</div>
			</div>
			<div style="margin-left:208px;">
				<div id="colTableOuter" class="concui-datatable-outer">
					<table id="colTable" class="concui-datatable">
						<thead>
							<th style="width:150px;text-align:center;">字段中文名</th>
							<th style="width:150px;text-align:center;">字段名</th>
							<th style="width:100px;text-align:center;">字段类型</th>
							<th style="width:100px;text-align:center;">长度</th>
							<th style="width:150px;text-align:center;display:none;">ID</th>
						</thead>
					</table>
				</div>
			</div>
		</div>
	</div>
	
	<script src="static/js/platform/component/jQuery/jQuery-1.8.2/jquery-1.8.2.js" type="text/javascript"></script>
	<script type="text/javascript" src="static/js/concui/concuiDataTable/concuiDataTable.js"></script>
	<script type="text/javascript" src="avicit/cbb/techclass/tabledefine/commtablemanager/custom_function.js"></script>
	<script type="text/javascript">
		var initColNameStr = ${initColNameStr};
		var colId = "${cbbTabcolumns.id}";
		var nodeId = "${nodeId}";
		var sysId = "${sysId}";
		var funcDataTable=null;
		var colDataTable=null;
		var isHigh=false;//是否是高级
		var colLabel="${cbbTabcolumns.colLabel}";
		var ruleValueShow="";
		var ruleText="";
		
		window.onload=function(){
			setColItem("col1");
			setColItem("col2");
			setColItem("col3");
			//加载标准函数数据
			funcDataTable = new concuiDataTable({
				id:"funcTable",
				url:"platform/sysstandardfunction/SysStandardFunctionController/getAll",
				cols:[
					{name:"functionChinese",editType:"none"},
					{name:"functionEnglish",editType:"none",hide:true}
				],
				idKey:"id",
				enableEdit:false,
				onRowMousedown:function(table, row){
					setRuleHighText(' '+row.cells[0].innerText+' ');
				}
			});
			
			//加载字段数据
			colDataTable = new concuiDataTable({
				id:"colTable",
				url:"platform/sysstandardfunction/SysStandardFunctionController/getColumns",
				params:{id:colId,nodeId:nodeId,sysId:sysId},
				cols:[
					{name:"COL_LABEL",editType:"none"},
					{name:"COL_NAME",editType:"none"},
					{name:"COL_TYPE",editType:"none"},
					{name:"COL_LENGTH",editType:"none"},
					{name:"ID",editType:"none",hide:true}
				],
				enableEdit:false,
				onRowMousedown:function(table, row){
					setRuleHighText(' '+row.cells[0].innerText+' ');
				}
			});
			//alert(contains("adfsdf","f4"));
		}
		
		function setColItem(id){
			var colSelect = document.getElementById(id);
			colSelect.options.length=0;
			for(var i=0,len=initColNameStr.length;i<len;i++){
				colSelect.options.add(new Option(initColNameStr[i].lookupName,initColNameStr[i].lookupCode));
			};
		}
		
		function colSelectChange(sender){
			var rs="";
			var showRule = document.getElementById("showRule");
			var col1 = document.getElementById("col1");
			var col2 = document.getElementById("col2");
			var col3 = document.getElementById("col3");
			var staticStr1 = document.getElementById("staticStr1");
			var staticStr2 = document.getElementById("staticStr2");
			if(col1.value!=""){
				rs+=" + "+getSelectText(col1);
			}
			if(staticStr1.value.trim()!=""){
				rs+=" + '"+staticStr1.value+"'";
			}
			if(col2.value!=""){
				rs+=" + "+getSelectText(col2);
			}
			if(staticStr2.value.trim()!=""){
				rs+=" + '"+staticStr2.value+"'";
			}
			if(col3.value!=""){
				rs+=" + "+getSelectText(col3);
			}
			showRule.innerHTML=colLabel+":="+rs.substring(2);
		}
		
		function getSelectText(sender){
			for(var i=0,len=sender.options.length;i<len;i++){
				if(sender.options[i].selected==true){
					return sender.options[i].text;
				};
			}
			return "";
		}
		
		function setRuleHighText(text){
			var showRuleHigh = document.getElementById("showRuleHigh");
			Editor.setSelectText(showRuleHigh, text);
		}
		
		//验证规则
		function validRule(){
			try{
				var showRuleHigh = document.getElementById("showRuleHigh");
				ruleText=showRuleHigh.value;
				if(!isHigh){
					ruleText = document.getElementById("showRule").value.replace(colLabel+":=","");
				}
				if(ruleText.trim()==""){
					alert("生成规则为空");
					return false;
				}
				ruleValueShow = ruleText;
				var ruleValue = ruleText;
				//替换字段
				var colData = colDataTable.getData();
				for(var i=0,len=colData.length;i<len;i++){
					if(ruleValue.indexOf(colData[i].COL_LABEL)>-1){
						var colId = colData[i].COL_NAME.replace(".","_");
						ruleValueShow = ruleValueShow.replaceAll(colData[i].COL_LABEL, colData[i].COL_NAME);
						ruleValue = ruleValue.replaceAll(colData[i].COL_LABEL, "document.getElementById('"+colId+"').value");
						var con = document.getElementById(colId);
						if(!con){
							var value="";
							if(colData[i].COL_TYPE=="date"){
								value="2019-08-03";
							}else if(colData[i].COL_TYPE=="integer"){
								value="1";
							}
							createInput(colId,value);
						}
					}
				}
				//替换函数
				var funcData = funcDataTable.getData();
				for(var i=0,len=funcData.length;i<len;i++){
					var funcTitle=funcData[i].functionChinese;
					var funcName = funcData[i].functionEnglish;
					funcTitle = funcTitle.substring(0,funcTitle.indexOf("(")+1);
					funcName = funcName.substring(0,funcName.indexOf("(")+1);
					if(ruleValue.indexOf(funcTitle)>-1){
						ruleValueShow = ruleValueShow.replaceAll(funcTitle.replace("(","\\("), funcName);
						ruleValue = ruleValue.replaceAll(funcTitle.replace("(","\\("), funcName);
					}
				}
				
				var rs = eval(ruleValue);
				alert("验证成功！\n"+ruleValueShow);
				return true;
			}catch(e){
				alert("验证失败！\n"+ruleValueShow);
				return false;
			}
		}
		
		document.onmouseup=function(){
			if(isHigh){
				document.getElementById("showRuleHigh").focus();
			}
		}
	</script>
	
	<script type="text/javascript">
		var dialogId='<%=request.getParameter("dialogId")%>';
		//确定
		function accept(){
			if(validRule()){
				// 将设置参数传回父页面统一保存
				colGeneMethodRuleText=ruleText;
				colGeneMethodRule = ruleValueShow;
				parent.idSet = colId;
				parent.colGeneMethodSet = 4;
				parent.colGeneMethodRuleSet = colGeneMethodRule;
				parent.colGeneMethodRuleText = colGeneMethodRuleText;
				parent.groupJson = "[]";
				// 执行父页面保存方法
				parent.accept();
				// 关闭子页面
				if (parent != null && parent.$('#' + dialogId) != null) {
					parent.$('#' + dialogId).dialog('close');
				}
			}
		}
		//验证
		function valid(){
			validRule();
		}
		//重置
		function reset(){
			document.getElementById("showRuleHigh").innerHTML="";
		}
		function highSet(){
			document.getElementById("normalSet").style.display="none";
			document.getElementById("highSet").style.display="";
			document.getElementById("btnValid").style.display="";
			document.getElementById("btnReset").style.display="";
			document.getElementById("btnHighSet").style.display="none";
			isHigh=true;
			var showRuleHigh = document.getElementById("showRuleHigh");
			var showRule = document.getElementById("showRule");
			showRuleHigh.focus();
			showRuleHigh.innerHTML=showRule.value.replace(colLabel+":=","");
			setHeight();
		}
		
		//返回
		function closeWindow() {
			if (parent != null && parent.$('#' + dialogId) != null) {
				// 父页面数据还原
				var parentDatagrid = parent.$("#cbbTabColumnsDataGrid");
				var row = parentDatagrid.datagrid('getSelected');
				var index = parentDatagrid.datagrid('getRowIndex', row);
				parentDatagrid.datagrid('endEdit', index);
				parentDatagrid.datagrid('addEditor', { // 添加【生成方式规则显示】编辑器
					field : 'colValueName',
					editor : {type : 'text'}
				});
				parentDatagrid.datagrid('beginEdit', index);
				// 获取生成方式对应editor
				var editors = parentDatagrid.datagrid('getEditors', index);
				editors[5].target.combobox('setValue', parent.colGeneMethodOld); // 生成方式
				editors[6].target.val(parent.colGeneMethodRuleOld); // 生成方式隐藏值
				editors[7].target.val(parent.colValueNameOld); // 生成方式显示值
				editors[7].target.prop('readonly', true); // 设置只读
				parentDatagrid.datagrid('endEdit', index);
				parent.$('#' + dialogId).dialog('close');
			};
		}
		
		function setHeight(){
			var pageH = window.innerHeight||document.documentElement.clientHeight||document.body.clientHeight;
			var toolbarH=document.getElementById("toolbar").offsetHeight;
			var highSetTopH=document.getElementById("highSetTop").offsetHeight;
			var funcTableOuter=document.getElementById("funcTableOuter");
			var colTableOuter=document.getElementById("colTableOuter");
			var h = pageH - toolbarH - highSetTopH-6;
			funcTableOuter.style.height=h+"px";
			colTableOuter.style.height=h+"px";
		}
		
		window.onresize=function(){
			setTimeout(function(){
				setHeight();
			},300);
		}
		
		function createInput(id, value, name, type) {
			var input = document.createElement("input");
			input.id=id;
			if(typeof type!="undefined"){
				input.setAttribute("type",type);
			}else{
				input.setAttribute("type","hidden");
			}
			if(typeof name!="undefined"){
				input.setAttribute("name",name);
			}
			
			input.setAttribute("value", value);
			document.body.appendChild(input);
			return input;
		};
	</script>
	
	<script type="text/javascript">
		var Editor = {
			__calcBookmark: function(bookmark) {
				return (bookmark.charCodeAt(0) - 1) + (bookmark.charCodeAt(3) - 1) * 65536 + (bookmark.charCodeAt(2) - 1);
			},
			__getSelectPos: function(editor, end) {
				if (!editor) return;
				if (typeof editor.selectionStart != "undefined")
					return end ? editor.selectionEnd : editor.selectionStart;
				if (!editor.createTextRange) return;
				editor.focus ();
				var range = document.selection.createRange().duplicate();
				if (!end) range.collapse(true)
				range.setEndPoint("StartToEnd", range);
				var start = document.body.createTextRange();
				start.moveToElementText(editor);
				var start = this.__calcBookmark(range.getBookmark()) - this.__calcBookmark(start.getBookmark());
				var value = editor.value;
				return start;
			},
			getSelectStart: function(editor) {
				return this.__getSelectPos(editor);
			},
			getSelectEnd: function(editor) {
				return this.__getSelectPos(editor, true);
			},
			getSelectRange: function(editor) {
				return [this.getSelectStart(editor), this.getSelectEnd(editor)];
			},
			setSelectRange: function(editor, range) {
				if (!editor) return;
				if (range[0] > range[1]) return;
				editor.focus();
				if (editor.setSelectionRange) {
					editor.setSelectionRange(range[0], range[1]);
				} else if (editor.createTextRange) {
					var value = editor.value;
					var textRange = editor.createTextRange();
					textRange.collapse(true);
					textRange.moveEnd("character", range[1]);
					textRange.moveStart("character", range[0]);
					textRange.select();
				}
			},
			textPos: function(editor, pos) {
				if (!editor) return;
				if (!editor.createTextRange) return pos;
				var value = editor.value;
				for (var i = 0; i <= pos; i++) if (value.charAt(i) == "\n") pos++;
				return pos;
			},
			cartPos: function(editor, pos) {
				if (!editor) return;
				if (!editor.createTextRange) return pos;
				var value = editor.value;
				var j = 0;
				for (var i = 0; i <= pos; i++) if (value.charAt(i) == "\n") j++;
				return pos - j;
			},
			selectEx: function(editor, startPattern, endPattern, include) {
				if (!editor) return;
				startPattern = "" + startPattern;
				endPattern = "" + endPattern;
				var range = this.getSelectRange(editor);
				var value = editor.value;
				var textRange = [this.textPos(editor, range[0]), this.textPos(editor, range[1])];
				var startStr = value.substr(0, textRange[0]);
				var endStr = value.substring(textRange[1], value.length);
				var i = startStr.lastIndexOf(startPattern);
				if (i < 0) return;
				var j = endStr.indexOf(endPattern);
				if (j < 0) return;
				j += textRange[1];
				if (include)
					j += endPattern.length;
				else i += startPattern.length;
				this.setSelectRange(editor, [this.cartPos(editor, i), this.cartPos(editor, j)]);
			},
			setSelectText: function (editor, value, range) {
				if (!editor) return;
				editor.focus();
				if (range && range.parentElement() == editor) {
					range.text = value;
					range.select();
					range = null;
				} else if (typeof editor.selectionStart != "undefined") { // firefox
					var str = editor.value;
					var start = editor.selectionStart;
					var scroll = editor.scrollTop;
					editor.value = str.substr(0, start) + value +
						str.substring(editor.selectionEnd, str.length);
					editor.selectionStart = start + value.length;
					editor.selectionEnd = start + value.length;
					editor.scrollTop = scroll;
				}else if (document.selection) {
					var range = document.selection.createRange();
					range.text = value;
					range.moveEnd("character",0);
					range.select();
				};
			}
		};
	</script>
</body>
</html>